package com.example.ex2.persistence;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.ex2.domain.Returninfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface ReturninfoMapper extends BaseMapper<Returninfo> {
    @Select("SELECT i.id, i.return_state, i.borrow_time,i.return_time, u.username AS username,  it.name AS itemname " +
            "FROM info i " +
            "LEFT JOIN user u ON i.user_id = u.user_id " +
            "LEFT JOIN item it ON i.item_id = it.item_id ")
    List<Returninfo> getallinfos();
    @Select("SELECT i.id, i.return_state, i.borrow_time,i.return_time, u.username AS username,  it.name AS itemname " +
            "FROM info i " +
            "LEFT JOIN user u ON i.user_id = u.user_id " +
            "LEFT JOIN item it ON i.item_id = it.item_id " +
            "WHERE i.return_state=1")
    List<Returninfo> getReturninfos();
    @Select("SELECT i.id, i.return_state, i.borrow_time,i.return_time,i.user_id,i.item_id " +
            "FROM info i " +
            "WHERE i.user_id=#{user_id} and i.item_id=#{item_id}")
    Returninfo getReturninfosByUser_idAndItem_id(int user_id,int item_id);

    @Select("SELECT i.id, i.return_state, i.borrow_time,i.return_time,i.user_id,i.item_id " +
            "FROM info i " +
            "WHERE i.id=#{id} ")
    Returninfo getReturninfosByid(int id);
    @Select("SELECT i.id, i.return_state, i.borrow_time,i.return_time, u.username AS username,  it.name AS itemname " +
            "FROM info i " +
            "LEFT JOIN user u ON i.user_id = u.user_id " +
            "LEFT JOIN item it ON i.item_id = it.item_id " +
            "WHERE i.id = #{id} and i.return_state=1")
    Returninfo getReturninfosById(int id);
    @Select("SELECT i.id " +
            "FROM info i " +
            "WHERE i.id = #{id} and i.return_state=1")
    Integer deleteReturninfosById(int id);



    @Insert("UPDATE info SET user_id = #{user_id}, item_id = #{item_id}, " +
            "return_state = #{return_state}, borrow_time = #{borrow_time}, return_time = #{return_time} " +
            "WHERE id = #{id}")
    boolean addReturninfo(int id, int user_id, int item_id, int return_state,Date borrow_time, Date return_time);

    @Update("UPDATE info SET user_id = #{user_id}, item_id = #{item_id}, " +
            "return_state = #{return_state}, borrow_time = #{borrow_time}, return_time = #{return_time} " +
            "WHERE id = #{id}")
    boolean updateReturninfo(int id, int user_id, int item_id, int return_state,Date borrow_time, Date return_time);


}
